<?php
namespace app\manage\controller;

use app\manage\controller\Base;
use think\facade\Db;
use support\View;
use support\Request;
use app\model\UserModel;

class UserController extends Base
{
    public function index(Request $request)
    {
        $user=new UserModel();
        $key=$request->input('key','');
        View::assign('key', $key);
        $order='id desc';
        $user=$user
            ->where('name|phone', 'like', '%'.$key.'%')
            ->order($order)->paginate(['list_rows'=>10,'query'=>request()->all()])->each(function ($item, $key) {
                $shang= Db::name('user')->where('id', $item->fid)->find();
                $item->fname = isset($shang) ? ($shang['name'] ?: $shang['phone']) : '';
                $item->xia = Db::name('user')->where('fid', $item->id)->count('id');
            });
        View::assign('user', $user);
        View::assign('count1', $user->total());
        return view('user/index');
    }
	
    public function add(Request $request)
    {
        if (request()->method()=='POST') {
            $data=$request->post();
            if (!($data['f_phone'] and preg_match("/^1[3456789]{1}\d{9}$/", $data['f_phone']))) {
                return view_e('上一级手机号不正确');
            }
            if (!($data['phone'] and preg_match("/^1[3456789]{1}\d{9}$/", $data['phone']))) {
                return view_e('填写手机号不正确');
            }
            if (!$f_user=Db::name('user')->where('phone', $data['f_phone'])->find()) {
                return view_e('找不到上一级信息');
            }
            if (Db::name('user')->where('phone', $data['phone'])->find()) {
                return view_e('手机号已经存在不能重复添加');
            }
            $msg=123456;
            $info=[
                'phone'=>$data['phone'],
                'yzm'=>$msg,
                'fid'=>$f_user['id'],
                'password'=>md5(sha1($msg))
            ];
            $u=UserModel::create($info);
            Db::name('user')->where('id', $u->id)->update(['user_no'=>$this->sui($u->id)]);
            return view_s('添加成功');
        }
        return view('user/add');
    }
    public function ajax(Request $request)
    {
        $data=$request->all();
        $user=new UserModel();
        if ($data['type']=='user_start') {
            if (UserModel::where('id', $data['id'])->update(['isopen'=> 1])) {
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
        }
        if ($data['type']=='user_stop') {
            if (UserModel::where('id', $data['id'])->update(['isopen'=> 0])) {
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
        }
        if ($data['type']=='user_fid') {
            if ($wo=UserModel::where('fid', $data['id'])->select()->toArray()) {
                return json($wo);
            } else {
                return json([]);
            }
        }
        if ($data['type']=='user_xuan') {
            if ($data['zi']==$data['id']) {
                return json(['code'=>0,'msg'=>'自己不能为自己下一级']);
            }
                        
            if (UserModel::where('id', $data['zi'])->update(['fid'=>$data['id']])) {
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
        }
        return json(['code'=>0,'msg'=>'失败']);
    }
    public function lit(Request $request)
    {
        $user=new UserModel();
        $key=$request->input('key', '');
        $id=$request->input('id', 0);
        View::assign('key', $key);
        $user=$user->where('fid', $id)
            ->where('nickName', 'like', '%'.$key.'%')
            ->order('id desc')->paginate(['list_rows'=>10,'query'=>request()->all()]);
        View::assign('user', $user);
        $count1=Db::name('user')->where('fid', $id)->count();
        View::assign('count1', $count1);
        return view('user/lit');
    }
    
    public function xiu(Request $request)
    {
        if (request()->method()=='POST') {
            $data=$request->post();
            if ($wo=Db::name('user')->where('id', $data['id'])->find()) {
                if (!$wo['user_no']) {
                    Db::name('user')->where('id', $wo['id'])->update(['user_no'=>$this->sui($wo['id'])]);
                }
                Db::name('user')->where('id', $wo['id'])->update(['password'=>md5(sha1($data['password']))]);
                return view_s('修改成功', url('user/index', ['st'=>1]));
            } else {
                return view_e('修改失败了');
            }
        }
        return view('user/xiu', [
                'data'=>Db::name('user')->find($request->get('id',0))
            ]);
    }
    private function sui($id=0)
    {
        $str=[
                0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
            ];
        $sui='';
        for ($i=0;$i<5;$i++) {
            $sui.=$str[mt_rand(0, 35)];
        }
        
        return $sui.$id;
    }
    public function lit2(Request $request)
    {
        $id=$request->get('id', 0);
        $user=Db::name('user')->where('fid', $id)->order('id desc')->select();
        View::assign('user', $user);
        return view('user/lit2');
    }
}
